package cn.cinz.service;

import cn.cinz.dao.SendSmsMapper;
import cn.cinz.util.RedisUtil;
import cn.cinz.util.SendSmsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;

public class SendSmsServiceImpl implements SendSmsService{

    SendSmsMapper sendSmsMapper;

    @Autowired
    public void setSendSmsMapper(SendSmsMapper sendSmsMapper) {
        this.sendSmsMapper = sendSmsMapper;
    }

    /**
     * 每个手机只能发送三次，验证码放到redis中，设置过期时间为120m
     * @param userPhone 手机号
     */
    @Override
    public String verifyCode(String userPhone) {
        String verifyCodeMsg = "";
        //Jedis jedis = null;
        Jedis jedis = RedisUtil.getRedis();
        try {
            //连接redis
            //部署上服务器时使用 127.0.0.1   本地使用服务器ip 121.199.62.53
            jedis = new Jedis("121.199.62.53",6379);
            //手机发送次数key
            String countKey = "PhoneNumbers:"+userPhone+",count:";
            //验证码key
            String codeKey = "PhoneNumbers:"+userPhone+",code:";

            //每个手机号一个小时发送次数不能超过三次  （待实现：一分钟只能发送一次）
            String count = jedis.get(countKey);
            if (count == null){
                //没有发送，第一次发送
                //设置发送次数为1
                jedis.setex(countKey,1*60*60,"1");
                verifyCodeMsg = "发送成功";
            }else if(Integer.parseInt(count)<= 2){
                //发送次数+1
                jedis.incr(countKey);
                verifyCodeMsg = "发送成功";
            }else if (Integer.parseInt(count) > 2){
                //发送三次，不能再发送
                System.out.println("一个小时发送次数不能超过三次");
                jedis.close();
                verifyCodeMsg =  "一个小时发送次数不能超过三次";
            }

            String code = SendSmsUtil.getSendSms(userPhone);
            System.out.println(code);
            jedis.setex(codeKey,300,code);

        } catch (NumberFormatException e) {
            e.printStackTrace();
        } finally {
            if (jedis != null){
                jedis.close();
            }
        }
        return verifyCodeMsg;
    }


    /**
     * 验证码校验
     * @param userPhone 手机号
     * @param code 验证码
     * @return
     */
    @Override
    public String getRedisCode(String userPhone, String code) {
//        Jedis jedis = null;
        Jedis jedis = RedisUtil.getRedis();
        try {
            //连接redis 从redis获取验证码
            //jedis = new Jedis("121.199.62.53",6379);
            //验证码key
            String codeKey = "PhoneNumbers:"+userPhone+",code:";
            String redisCode = jedis.get(codeKey);
            System.out.println(redisCode);
            //判断
            if (redisCode.equals(code)){
                System.out.println("成功");
                return "成功";
            }else {
                System.out.println("失败");
                return "验证码错误或已失效";
            }
        } catch (Exception e) {
            return "验证码错误或已失效";
        } finally {
            if (jedis != null){
                jedis.close();
            }
        }

    }
}
